﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Toolbox
{
    public partial class HttpForm : Form
    {

        const string GET = "GET";
        const string POST = "POST";
        const string PUT = "PUT";
        const string DELETE = "DELETE";

        const string APPLICATION_FORM = "application/x-www-form-urlencoded";
        const string APPLICATION_JSON = "application/json";

        public HttpForm()
        {
            InitializeComponent();
            initData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 发送按钮
            string method = comboBox1.Text;

            clear();

            var client = new RestClient();
            var request = new RestRequest(textBox1.Text);

            addHeader(request);
            addCookie(request);
            addParam(request);

            //Console.WriteLine(request.Resource);

            List<Parameter> paramList = request.Parameters;
            textBox6.Text = string.Join("&", paramList);

            IRestResponse response = null;

            switch (method)
            {
                case GET:
                    response = client.Get(request);
                    break;
                case POST:
                    response = client.Post(request);
                    break;
                case PUT:
                    response = client.Put(request);
                    break;
                case DELETE:
                    response = client.Delete(request);
                    break;
                default:
                    break;
            }

            textBox2.Text = response.StatusCode.ToString();
            textBox7.Text = response.Content;

        }

        private void initData()
        {
            // 初始化Header
            Dictionary<string, Object> header = new Dictionary<string, Object>();
            header.Add("token", "value");
            textBox3.Text = JsonConvert.SerializeObject(header);

            // 初始化Cookie
            Dictionary<string, Object> cookie = new Dictionary<string, Object>();
            cookie.Add("name", "value");
            textBox4.Text = JsonConvert.SerializeObject(cookie);

            // 初始化参数
            Dictionary<string, Object> param = new Dictionary<string, Object>();
            param.Add("param", "value");
            textBox5.Text = JsonConvert.SerializeObject(param);

            // 初始化方法下拉框
            comboBox1.DataSource = new string[] { GET, POST, PUT, DELETE };
            comboBox1.SelectedIndex = 0;

            // 初始化参数类型下拉框
            comboBox2.DataSource = new string[] { APPLICATION_FORM, APPLICATION_JSON };
            comboBox2.SelectedIndex = 0;

            /*Dictionary<string, Object> dict = new Dictionary<string, Object>();
            dict.Add("name", "wushuqing");
            dict.Add("age", 18);
            string json = JsonConvert.SerializeObject(dict);
            Console.WriteLine(json);

            Dictionary<string, Object> dict2 = new Dictionary<string, Object>();
            dict2 = JsonConvert.DeserializeObject<Dictionary<string, Object>>(json);
            foreach (var item in dict2)
            {
                Console.WriteLine("key:{0} value:{1}", item.Key, item.Value);
            }*/
        }

        private void clear()
        {
            // 清除内容
            textBox6.Text = "";
            textBox2.Text = "";
            textBox7.Text = "";
        }

        private void addHeader(RestRequest request)
        {
            // 添加Header
            var header = textBox7.Text;
            if (header.Length > 0)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(header);
                foreach (var item in dict)
                {
                    request.AddHeader(item.Key, item.Value);
                }
            }
        }

        private void addCookie(RestRequest request)
        {
            // 添加Cookie
            var cookie = textBox4.Text;
            if (cookie.Length > 0)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(cookie);
                foreach (var item in dict)
                {
                    request.AddCookie(item.Key, item.Value);
                }
            }
        }

        private void addParam(RestRequest request)
        {
            // 添加Header
            var param = textBox5.Text;
            if (param.Length > 0)
            {
                Dictionary<string, Object> paramDict = new Dictionary<string, Object>();
                paramDict = JsonConvert.DeserializeObject<Dictionary<string, Object>>(param);

                if (GET == comboBox1.Text)
                {
                    foreach (var item in paramDict)
                    {
                        request.AddParameter(item.Key, item.Value);
                    }
                }
                else
                {
                    if (APPLICATION_FORM == comboBox2.Text)
                    {
                        foreach (var item in paramDict)
                        {
                            request.AddParameter(item.Key, item.Value);
                        }
                    }
                    else if (APPLICATION_FORM == comboBox2.Text)
                    {
                        request.AddParameter("application/json", param, ParameterType.RequestBody);
                    }
                }
                
            }
        }
    }
}
